package run.siyuan.manag.controller.admin;


import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import run.siyuan.common.entity.Result;
import run.siyuan.common.exception.ExceptionProperties;
import run.siyuan.manag.entity.SysDict;
import run.siyuan.manag.entity.SysRole;
import run.siyuan.manag.entity.SysUser;
import run.siyuan.manag.entity.vo.SysUserVo;
import run.siyuan.manag.service.SysDeptService;
import run.siyuan.manag.service.SysRoleService;

import java.util.List;

/**
 * <p>
 *  角色表  前端控制器
 * </p>
 *
 * @author siyuan
 * @since 2021-01-13
 */
@Api(tags = "角色管理")
@RestController
@RequestMapping("/manag/sys-role")
public class SysRoleController {

    @Autowired
    private SysRoleService roleService;

    @Autowired
    private ExceptionProperties properties;

    @ApiOperation("新增或修改")
    @PostMapping("saveOrUpdate")
    public Result saveOrUpdate(@RequestBody SysRole sysRole) {
        boolean result = false;
        if (StrUtil.isBlank(sysRole.getId())) {
            result = roleService.save(sysRole);
        } else {
            result = roleService.updateById(sysRole);
        }
        if (!result) {
            return Result.error(50000, properties.getCodeName("50000"));
        }
        return Result.success();
    }

    @ApiOperation(value = "详情", response = SysRole.class)
    @GetMapping("/getOne/{id}")
    public Result getDictById(@PathVariable Long id) {
        if (ObjectUtil.isEmpty(id)) {
            return Result.error(50001, properties.getCodeName("50001"));
        }
        return Result.success().data(roleService.getById(id));
    }

    @ApiOperation(value = "列表", response = SysRole.class)
    @GetMapping("/list")
    public Result list() {
        return Result.success().data(roleService.list());
    }


    @ApiOperation(value = "分页",response = SysRole.class)
    @GetMapping("/page/{page}/{limit}")
    public Result page(@ApiParam(value = "当前页码", required = true, defaultValue = "1") @PathVariable Long page,
                              @ApiParam(value = "每页记录数", required = true, defaultValue = "10") @PathVariable Long limit) {
        try {
            Page<SysRole> requestPage = new Page<>(page, limit);
            return new Result().data(roleService.page(requestPage,null));
        } catch (Exception e) {
            return new Result<>().data(e.getMessage());
        }
    }

    @ApiOperation("批量删除")
    @DeleteMapping("/batchDelete")
    public Result batchDelete(@RequestBody List<String> idList) {
        if (!roleService.removeByIds(idList)) {
            return Result.error(50001, properties.getCodeName("50001"));
        }
        return Result.success();
    }

    // TODO 角色菜单

    // TODO 保存角色菜单

}

